package com.moshang.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moshang.blog.core.utils.RestResponse;
import com.moshang.blog.entity.TbArticleInfo;
import com.moshang.blog.entity.bo.LikeCount;
import com.moshang.blog.core.constant.MySysUser;
import com.moshang.blog.mapper.TbArticleInfoMapper;
import com.moshang.blog.mapper.TblGreatMapper;
import com.moshang.blog.service.RedisService;
import com.moshang.blog.service.TbArticleInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @program: blog
 * @description: 博客文章服务类
 * @author: moshang
 * @create: 2018-12-10 14:03
 **/
@Service
public class TbArticleInfoServiceImpl extends ServiceImpl<TbArticleInfoMapper, TbArticleInfo> implements TbArticleInfoService {

   @Resource
   private TblGreatMapper tblGreatMapper;
    @Resource
    private TbArticleInfoMapper tbArticleInfoMapper;
   @Resource
   private RedisService redisService;
    @Override
    public void saveOrUpdateArticle(TbArticleInfo tbArticleInfo) {
        if (tbArticleInfo.getId()==null) {

            tbArticleInfo.setCreateBy(MySysUser.currentUser().getId());
            tbArticleInfo.setUpdateBy(MySysUser.currentUser().getId());
            tbArticleInfo.setCreateTime(new Date());
            tbArticleInfo.setUpdateTime(new Date());
            tbArticleInfo.setCreateName(MySysUser.realName());
            tbArticleInfo.setUpdateName(MySysUser.realName());
            tbArticleInfo.setIsActivited(0);
        }else {
            tbArticleInfo.setUpdateBy(MySysUser.currentUser().getId());
            tbArticleInfo.setUpdateName(MySysUser.realName());
        }
        save(tbArticleInfo);
    }

    @Override
    public TbArticleInfo findById(Integer id) {
        QueryWrapper<TbArticleInfo> param=new QueryWrapper<>();
        param.eq("is_activited",0);
        param.eq("id",id);
        TbArticleInfo tbArticleInfo1=baseMapper.selectOne(param);
        if (tbArticleInfo1!=null){
            return tbArticleInfo1;
        }
        return null;
    }

    @Override
    public List<TbArticleInfo> findByIsTop() {
        QueryWrapper<TbArticleInfo> entityWrapper=new QueryWrapper<>();
        entityWrapper.eq("is_top",1);

        entityWrapper.eq("status",1);
        entityWrapper.orderByDesc("create_time");
        List<TbArticleInfo> list=baseMapper.selectList(entityWrapper);
        if (list.size()>10){
            return list.subList(0,9);
        }
        return list;
    }

    @Override
    public List<TbArticleInfo> findByIsRecommend() {
        QueryWrapper<TbArticleInfo> entityWrapper=new QueryWrapper<>();
        entityWrapper.eq("is_recommend",1);
        entityWrapper.eq("status",1);
        entityWrapper.orderByDesc("create_time");
        List<TbArticleInfo> list=baseMapper.selectList(entityWrapper);
        if (list.size()>5){
            return list.subList(0,4);
        }
        return list;
    }

    @Override
    public RestResponse updateInfo(Integer articleId, Integer useId) {
        RestResponse restResponse=new RestResponse();
        restResponse.setCode(RestResponse.success());
        TbArticleInfo article=findById(articleId);
        Integer code=redisService.findLikedFromRedis(useId,articleId);
        if (code!=null && code==1){
            //如果找到了这条记录，取消点赞
            redisService.unlikeFromRedis(useId,articleId);
            //文章点赞数减1
          redisService.decrementLikedCount(articleId);
          restResponse.setMsg("取消点赞");
        }else {
            redisService.saveLiked2Redis(useId,articleId);
            redisService.incrementLikedCount(articleId);
            restResponse.setMsg("点赞成功");
        }
        article.setSupport(redisService.findLikedCount(articleId));
        restResponse.setData(article);
        return restResponse;
    }

    @Override
    public List<TbArticleInfo> findNear(Integer id, Integer sortId) {
        QueryWrapper<TbArticleInfo> entityWrapper=new QueryWrapper<>();
        entityWrapper.ne("id",id);
        entityWrapper.eq("sort_id",sortId);
        entityWrapper.orderByDesc("create_time");
        List<TbArticleInfo> list=baseMapper.selectList(entityWrapper);
        if (list.size()>5){
            return list.subList(0,4);
        }
        return list;
    }

    @Override
    public void saveCountDb() {
        //查询点赞总量
        List<LikeCount> list=redisService.getLikedCountFromRedis();
        if (list.size()>0) {
            for (LikeCount likeCount : list) {
                TbArticleInfo tbArticleInfo1 = baseMapper.selectById(likeCount.getArticleId());
                if (tbArticleInfo1 != null )
                    tbArticleInfo1.setSupport(likeCount.getCount());
                tbArticleInfoMapper.saveCountDb(likeCount.getArticleId(), likeCount.getCount());
            }
        }
    }

    @Override
    public List<Map> findCountBySort() {
        return tbArticleInfoMapper.findCountBySort();
    }

}
